package com.controller.UserController;

import com.UserReq.UserLoginReq;
import com.entity.User;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //登录
    @PostMapping("/login")
    public int login(UserLoginReq userLoginReq){
        User user = new User();
        user.setUsername(userLoginReq.getUsername());
        user.setPassword(userLoginReq.getPassword());
        User backUser = userMapper.UserLoginCheck(user);
        int userId = Math.toIntExact(backUser.getId());
        return userId;
    }
    //注册
    @PostMapping("/register")
    public int register(@RequestParam String username, @RequestParam String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int i = userMapper.UserRegister(user);
        return i;
    }
    //获取整张user表
    @GetMapping("/userList")
    public List<User> getUserList(){
        return userMapper.selectList(null);
    }
    //通过userId查询那一条的用户信息
    @GetMapping("/userId/{id}")
    public User getUserById(@PathVariable long id){
        return  userMapper.selectById(id);
    }
    @PutMapping("/userUpdate")
    public int userUpdate(@RequestBody User user){
        return userMapper.updateById(user);
    }
    //删除
    @DeleteMapping("/user/{id}")
    public int userUpdate(@PathVariable long id){
        return userMapper.deleteById(id);
    }
}
